<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      size="small"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="judgeList">
      <el-table-column
        prop="id"
        label="ID"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="nickname"
        label="昵称"
        align="center"
      ></el-table-column>
      <el-table-column label="头像" width="100" align="center">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.gender === "male" ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="birthYear"
        label="出生年份"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="education"
        label="学历"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="location"
        label="居住地"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="registerTime"
        label="注册时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column prop="username" label="用户名" align="center" />
      <el-table-column prop="name" label="姓名" align="center" />
      <el-table-column prop="phone" label="手机号" align="center" />
      <el-table-column prop="age" label="年龄" width="80" align="center" />
      <el-table-column prop="income" label="收入" align="center" />
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '1' ? 'success' : 'danger'">
            {{ scope.row.status === "1" ? "正常" : "已冻结" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            type="text"
            :class="scope.row.status === '1' ? 'text-danger' : 'text-success'"
            @click="handleFreezeToggle(scope.row)"
          >
            {{ scope.row.status === "1" ? "冻结" : "解冻" }}
          </el-button>
          <el-button type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="dialogType === 'add'">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" :min="1" :max="120" />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历">
            <el-option label="高中" value="high_school" />
            <el-option label="专科" value="college" />
            <el-option label="本科" value="bachelor" />
            <el-option label="硕士" value="master" />
            <el-option label="博士" value="doctor" />
          </el-select>
        </el-form-item>
        <el-form-item label="收入" prop="income">
          <el-input v-model="form.income" placeholder="请输入年收入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog
      title="导入评委"
      :visible.sync="importVisible"
      width="400px"
      append-to-body
    >
      <el-upload
        :action="uploadUrl"
        :headers="headers"
        :on-success="handleImportSuccess"
        :on-error="handleImportError"
        accept=".xlsx, .xls"
        class="upload-demo"
      >
        <el-button type="primary">选择文件</el-button>
        <div slot="tip" class="el-upload__tip">只能上传xlsx/xls文件</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJudges,
  addJudge,
  updateJudge,
  removeJudge,
  freezeJudge,
  unfreezeJudge,
} from "@/api/judge";
import Pagination from "@/components/Pagination";
import { getToken } from "@/utils/auth";

export default {
  name: "JudgeManagement",
  components: {
    Pagination,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评委列表
      judgeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: undefined,
        username: undefined,
        name: undefined,
        phone: undefined,
      },
      dialogVisible: false,
      dialogType: "add", // add或edit
      dialogTitle: "",
      importVisible: false,
      uploadUrl: process.env.VUE_APP_BASE_API + "/judge/import",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      form: {
        id: undefined,
        username: "",
        name: "",
        gender: "male",
        phone: "",
        password: "",
        age: 18,
        education: "",
        income: "",
        status: "1",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评委列表 */
    getList() {
      this.loading = true;
      listJudges(this.queryParams)
        .then((response) => {
          this.judgeList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialogType = "add";
      this.dialogTitle = "新增评委";
      this.form = { ...this.$options.data().form };
      this.dialogVisible = true;
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.dialogType = "edit";
      this.dialogTitle = "编辑评委";
      this.form = { ...row };
      this.dialogVisible = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除该评委？")
        .then(() => {
          return removeJudge(row.id);
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
    handleImport() {
      this.importVisible = true;
    },
    handleImportSuccess(response) {
      this.$modal.msgSuccess("导入成功");
      this.importVisible = false;
      this.getList();
    },
    handleImportError() {
      this.$modal.msgError("导入失败");
    },
    handleFreezeToggle(row) {
      const isFreeze = row.status === "1";
      const text = isFreeze ? "冻结" : "解冻";
      this.$modal
        .confirm(`确认要${text}该评委吗？`)
        .then(() => {
          return isFreeze ? freezeJudge(row.id) : unfreezeJudge(row.id);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
          this.getList();
        });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const submitFn = this.form.id ? updateJudge : addJudge;
          submitFn(this.form).then(() => {
            this.$modal.msgSuccess(
              this.dialogType === "add" ? "新增成功" : "修改成功"
            );
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.text-danger {
  color: #f56c6c;
}
.text-success {
  color: #67c23a;
}
</style>
